home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / XQLFUNCS.C < prev    next >
Text File  |  1993-06-11  |  7KB  |  348 lines

  1. /**************************************************
  2. * FILE NAME:  XQLfuncs.c   TITLE:  SQL database API
  3. *
  4. * AUTHOR:  K. E. North II
  5. *
  6. * SYNOPSIS: 
  7. *
  8. *        application interface to Novell's XQL and 
  9. *        Netware SQL.
  10. *
  11. ***************************************************/
  12.  
  13. #undef DEBUG    
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <string.h>
  19. #include <malloc.h>
  20.  
  21. #include "xqlintf.h"
  22.  
  23. #ifndef __XQLDEFS_H
  24.                     /*XQL-specific definitions */
  25. #include "xqldefs.h"
  26. #endif
  27.  
  28. #ifndef __XCOLUMN_H
  29.                     /* XQLM column description */
  30. #include "xcolumn.h"      
  31. #endif
  32.  
  33. #ifndef __XQLCALLS_H
  34.         /* function prototypes for XQLM calls */
  35. #include "xqlcalls.h"
  36. #endif
  37.  
  38. #ifndef __DBTYPES_H
  39. #include "dbtypes.h"
  40. #endif
  41.  
  42. #ifndef __XQLFUNCS_H
  43.         /* portable SQL API functions */
  44. #include "xqlfuncs.h"
  45. #endif
  46.  
  47.  
  48. /**************************************************
  49. * FUNCTION NAME: dbClose
  50. *
  51. * SYNOPSIS:
  52. *
  53. *  close a data base session - terminate connection
  54. *
  55. ***************************************************/
  56.  
  57. int dbClose()
  58. {
  59. int        ExitStatus;
  60.  
  61.     ExitStatus = dbDisconnect();    /* to logout */
  62.     if (ExitStatus)
  63.         return ExitStatus;
  64.     else 
  65.         return SQL_SUCCESS;
  66. }
  67.  
  68.  
  69. /**************************************************
  70. * FUNCTION NAME: dbRequest
  71. *
  72. * SYNOPSIS: 
  73. *
  74. *     prepare an SQL request / statement
  75. *
  76. *     for XQL/NWSQL:
  77. *
  78. *   XQLCursor  - request a cursor
  79. *                     (reserve resources)
  80. *    
  81. *    XQLCompile    - pass the SQL string to XQLM for
  82. *    compilation and execution (when appropriate)
  83. *
  84. ***************************************************/
  85.  
  86. int dbRequest( REQUEST rq )
  87. {
  88.  
  89. int      XQLStatus;
  90. int      StatementLength;
  91. HCUR  CursorID;
  92.  
  93.     XQLStatus   = XQLCursor (&CursorID);
  94.     if (XQLStatus)
  95.         {
  96.         printf 
  97.             ("XQLCursor failed, XQLStatus: %d\n", 
  98.                 XQLStatus);
  99.         return XQLStatus;
  100.         };
  101.                     /* save request's cursor */
  102.     rq->CursorID = CursorID;
  103.                      /* get length of */
  104.                     /* SQL command string */
  105.     StatementLength = strlen (rq->Statement);
  106.  
  107.     XQLStatus  = XQLCompile (rq->CursorID, 
  108.                     &StatementLength, 
  109.                     rq->Statement);
  110.     if (XQLStatus)
  111.         {
  112.         return XQLStatus;
  113.         }
  114.  
  115.     return SQL_SUCCESS;
  116. }
  117.  
  118. /**************************************************
  119. * FUNCTION NAME: dbFetch
  120. *
  121. * AUTHOR: Ken North 
  122. *
  123. * SYNOPSIS: 
  124. *
  125. *    
  126. *  XQLFetch    - return data from  XQLM
  127. *
  128. ***************************************************/
  129.  
  130. int dbFetch (VIEWINFO vu, REQUEST rq, char *records)
  131. {
  132. int         XQLStatus;
  133. int         ExitStatus;
  134.  
  135.  
  136.     XQLStatus = XQLFetch (rq->CursorID, 
  137.                 vu->FetchOption, &vu->BufferLen,
  138.                 records, &vu->ReceiveCount, 
  139.                 vu->ASCIIFlag, vu->Spacing);
  140.  
  141.     if (XQLStatus > 0)
  142.         {
  143.           return XQLStatus;
  144.            };
  145.  
  146.     vu->RecordsGotten = vu->ReceiveCount;
  147.     return SQL_SUCCESS;
  148. }
  149.  
  150. /**************************************************
  151. * FUNCTION NAME: dbGetData
  152. *
  153. * SYNOPSIS: 
  154. *
  155. *    get data base records via XQLM
  156. *    
  157. *   use defaults for FetchOption, Spacing and ASCII
  158. *    format flag
  159. *
  160. ***************************************************/
  161.  
  162. int dbGetData (VIEWINFO vu, 
  163.                 char *records, REQUEST rq)
  164.  
  165. {
  166. int     StatusReturned;
  167. int        XQLStatus;
  168.  
  169.     vu->ASCIIFlag = STOREZEROS;
  170.             /* put one binary 0 after each value */
  171.     vu->Spacing = 1; 
  172.  
  173.              /* ReceiveCount = number of records to 
  174.                     retrieve on each fetch    */
  175.     vu->BufferLen = vu->RowLength * 
  176.             vu->ReceiveCount; 
  177.  
  178.     StatusReturned = dbFetch ( vu, rq, records);
  179.     if (StatusReturned != SQL_SUCCESS)
  180.         {
  181.           return XQLStatus;
  182.            };
  183.  
  184.     return SQL_SUCCESS;
  185. }
  186.  
  187. /**************************************************
  188. * FUNCTION NAME: dbDescribe
  189. *
  190. * SYNOPSIS:     
  191. *
  192. *    access .ddf to retrieve list of fields
  193. *            via XQLDescribe
  194. *
  195. *    data types for XQL:
  196. *
  197. *        character    0        logical        7
  198. *        integer        1        numeric        8
  199. *        float        2        bfloat        9
  200. *        date        3        lstring        10
  201. *        time        4        zstring        11
  202. *        decimal        5        note        12
  203. *        money        6        lvar        13
  204. *
  205. ***************************************************/
  206.  
  207. int dbDescribe(int ColumnPos, 
  208.                 struct ColumnDesc *col, 
  209.                 REQUEST rq)
  210. {
  211. int      XQLStatus;
  212.  
  213.     col->namelen = NAMELENGTH;
  214.     col->data_type = 0;
  215.     col->size = 0; 
  216.     col->dec_places = 0;
  217.     col->display_len = 0; 
  218.     strcpy(col->fldname,"   ");
  219.  
  220.     XQLStatus = XQLDescribe (rq->CursorID, 
  221.             ColumnPos + 1, 
  222.             &col->data_type,
  223.             &col->size, 
  224.             &col->dec_places,
  225.             &col->display_len, 
  226.             &col->namelen,
  227.             col->fldname);
  228.  
  229.     if (XQLStatus)
  230.         return XQLStatus;
  231.     else
  232.         return SQL_SUCCESS;
  233.     
  234. }
  235.  
  236. /**************************************************
  237. * FUNCTION NAME: dbConnect
  238. *
  239. * SYNOPSIS:
  240. *             open an XQL/NWSQL connection
  241. *
  242. *
  243. *        XQLLogin()      - establish a session
  244. *                     XQL login to the data base
  245. *
  246. ***************************************************/
  247.  
  248. int dbConnect(CONNECTION si)
  249. {
  250. int        XQLStatus;
  251.  
  252.                 /********************************/
  253.                 /*   Login to XQLM / NWSQL        */
  254.                 /********************************/
  255.  
  256.     XQLStatus   = XQLLogin (si->userid, si->password, 
  257.                     si->dict_path, si->data_path, 
  258.                     si->reserved, 0);
  259.  
  260.     if (XQLStatus)
  261.         {
  262.         return XQLStatus;
  263.         };
  264.  
  265.     return SQL_SUCCESS;
  266. }
  267.  
  268.  
  269. /**************************************************
  270. * FUNCTION NAME: dbDisconnect
  271. *
  272. * SYNOPSIS: 
  273. *        exit an XQL/Netware SQL session
  274. *
  275. ***************************************************/
  276. int dbDisconnect()
  277. {
  278. int    XQLStatus;
  279.  
  280.     XQLStatus = XQLLogout ();
  281.     if (XQLStatus)
  282.         return XQLStatus;
  283.     else
  284.         return SQL_SUCCESS;
  285. }
  286.  
  287. /**************************************************
  288. * FUNCTION NAME: DecodeXQLDataType
  289. *
  290. * SYNOPSIS: return description of data type
  291. *
  292. ***************************************************/
  293. char *DecodeXQLDataType(int DataType)
  294. {
  295.     static char Type[20];
  296.  
  297.     strcpy(Type,"Unknown");
  298.     if (DataType == 0)
  299.         strcpy(Type,"character");
  300.     if (DataType == 1)
  301.         strcpy(Type,"integer");
  302.     if (DataType == 2)
  303.         strcpy(Type,"float");
  304.     if (DataType == 3)
  305.         strcpy(Type,"date");
  306.     if (DataType == 4)
  307.         strcpy(Type,"time");
  308.     if (DataType == 5)
  309.         strcpy(Type,"decimal");
  310.     if (DataType == 6)
  311.         strcpy(Type,"money");
  312.     if (DataType == 7)
  313.         strcpy(Type,"logical");
  314.     if (DataType == 8)
  315.         strcpy(Type,"numeric");
  316.     if (DataType == 9)
  317.         strcpy(Type,"bfloat");
  318.     if (DataType == 10)
  319.         strcpy(Type,"lstring");
  320.     if (DataType == 11)
  321.         strcpy(Type,"zstring");
  322.     if (DataType == 12)
  323.         strcpy(Type,"note");
  324.     if (DataType == 13)
  325.         strcpy(Type,"lvar");
  326.     return Type;
  327. }
  328.  
  329. #ifdef DEBUG
  330.  
  331. /**************************************************
  332. * FUNCTION NAME: pause()
  333. *
  334. * SYNOPSIS: prompt for key to continue
  335. *
  336. ***************************************************/
  337.  
  338. void pause(void)
  339. {
  340.     int    c;
  341.  
  342.     printf( "press a key to continue...\n" );
  343.     c = getch();
  344. }
  345.  
  346. #endif
  347.  
  348.